package com.hymall.service.wx;

import com.github.pagehelper.PageInfo;
import com.hymall.bean.*;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketAddressMapper;
import com.hymall.mapper.MarketRegionMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: 周子伟
 * @description ${}
 * @date: 2022/7/19 22:09
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public boolean save(MarketAddress marketAddress) {

        Date addtime = new Date();
        marketAddress.setAddTime(addtime);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        marketAddress.setUserId(userId);

        if (marketAddress.getIsDefault().equals(true)){
            addressMapper.updateByIsDefault();
        }

        // // 查找邮编
        // MarketRegionExample regionExample = new MarketRegionExample();
        // MarketRegionExample.Criteria criteria = regionExample.createCriteria();
        // criteria.andNameEqualTo(marketAddress.getProvince());
        // criteria.andTypeEqualTo((byte) 1);
        // List<MarketRegion> tempList = marketRegionMapper.selectByExample(regionExample);
        // regionExample.clear();
        //
        // if (!tempList.isEmpty()) {
        //     criteria = regionExample.createCriteria();
        //     criteria.andNameEqualTo(marketAddress.getCity());
        //     criteria.andPidEqualTo(tempList.get(0).getId());
        //     criteria.andTypeEqualTo((byte) 2);
        //     tempList = marketRegionMapper.selectByExample(regionExample);
        //     regionExample.clear();
        //     if (!tempList.isEmpty()) {
        //         criteria = regionExample.createCriteria();
        //         criteria.andNameEqualTo(marketAddress.getCounty());
        //         criteria.andPidEqualTo(tempList.get(0).getId());
        //         criteria.andTypeEqualTo((byte) 3);
        //         tempList = marketRegionMapper.selectByExample(regionExample);
        //         marketAddress.setPostalCode(tempList.get(0).getCode().toString());
        //     }
        // }

        if ((addressMapper.selectByPrimaryKey(marketAddress.getId()) == null)) {

            addressMapper.insertSelective(marketAddress);

        } else {

            Date updatetime = new Date();
            marketAddress.setUpdateTime(updatetime);
            addressMapper.updateByPrimaryKeySelective(marketAddress);
        }


        return true;
    }

    @Override
    public boolean delete(Integer id) {

        addressMapper.deleteByPrimaryKey(id);

        return true;
    }


    @Override
    public CommonData List() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andUserIdEqualTo(userId);
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);

        CommonData data = CommonData.data(pageInfo);


        return data;
    }

    @Override
    public MarketAddress detail(Integer id) {

        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);


        return marketAddress;
    }


}
